//
//  HomeDetailViewController.m
//  AmountBody
//
//  Created by smm on 2017/12/12.
//  Copyright © 2017年 RCMTM. All rights reserved.
//

#import "HomeDetailViewController.h"
#import "ADView.h"
#import "HomeDetailModel.h"
#import "HomeEssentialViewController.h"
//转场动画用
#import "XLBubbleTransition.h"

#define ADVIEW_TAG 60000
#define AdView_H SCREEN_W/(2048./700.)
@interface HomeDetailViewController ()<UIScrollViewDelegate>
//轮播图View
@property (strong, nonatomic) ADView *adView;
@property (strong, nonatomic) NSArray *AdArray;
@property (strong, nonatomic) NSMutableArray * dataArray;
@property (weak, nonatomic) IBOutlet UIButton *xuanFuButton;
- (IBAction)xuanFuButton:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UIImageView *navImage;
@property (weak, nonatomic) IBOutlet UIButton *backbutton;
- (IBAction)backbutton:(UIButton *)sender;

//顶部View
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *newsView;
@property (weak, nonatomic) IBOutlet UILabel *news_nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *news_contentLabel;
@property (weak, nonatomic) IBOutlet UILabel *news_countLabel;
@property (weak, nonatomic) IBOutlet UIImageView *news_countImage;
@property (weak, nonatomic) IBOutlet UIView *news_lineView;
@property (weak, nonatomic) IBOutlet UIView *CustomNewsView;
@property (weak, nonatomic) IBOutlet UIView *c_headView;
@property (weak, nonatomic) IBOutlet UIButton *c_headButton;
- (IBAction)c_headButton:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIImageView *c_headImage;
@property (weak, nonatomic) IBOutlet UIView *c_otherView;

@property (weak, nonatomic) IBOutlet UIView *PersonalCustomView;
@property (weak, nonatomic) IBOutlet UIView *p_headView;
@property (weak, nonatomic) IBOutlet UIButton *p_headButton;
- (IBAction)p_headButton:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIView *p_otherView;

@property (weak, nonatomic) IBOutlet UIImageView *p_headImage;
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (strong, nonatomic) NSMutableArray * customArray;
@property (strong, nonatomic) NSMutableArray * personalArray;

@end

@implementation HomeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.public_headView.hidden = NO;
    self.public_headView.leftButton = [self leftButton];
    self.public_headView.titleLabel = [self titleLabel];
    self.view.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    _webView.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    _webView.scrollView.delegate  =self;
    [self loadNavView];
    [self loadUI];
    //在ViewControllerA中添加push和pop的动画

    self.xl_pushTranstion = [XLBubbleTransition transitionWithAnchorRect:_xuanFuButton.frame];
    self.xl_popTranstion = [XLBubbleTransition transitionWithAnchorRect:_xuanFuButton.frame];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_adView startAnimation:NO];
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

-(void)backButtonPressed
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 懒加载
- (NSMutableArray *)customArray;
{
    if (!_customArray) {
        _customArray = [[NSMutableArray array]init];
    }
    return _customArray;
}
- (NSMutableArray *)dataArray;
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray array]init];
    }
    return _dataArray;
}
- (NSArray *)AdArray;
{
    if (!_AdArray) {
        _AdArray = [[NSArray alloc]init];
    }
    return _AdArray;
}

#pragma mark -  UI布局
//-(void)pushMethod{
//    HomeEssentialViewController * tmpVC = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"HomeEssentialViewController"];
//    tmpVC.title = @"基本信息";
//    [self.navigationController pushViewController:tmpVC animated:YES];
//    
//}

-(void)loadNavView
{
    CGFloat jianGeW = 25;
    CGFloat jianGeH = 20;
    CGFloat buttonW = 44;
    CGFloat buttonH = 44;
    CGFloat imageW = 36;
    CGFloat imageH = imageW;
    _navView.frame = CGRectMake(0, 0, SCREEN_W, 0);
    _navView.backgroundColor  =RGBColorAlpha(44, 44, 44, 1);
    _backbutton.frame = CGRectMake(jianGeW, jianGeH, buttonW, buttonH);
    _backbutton.imageView.contentMode = UIViewContentModeScaleAspectFit;
//    _backbutton.imageEdgeInsets = UIEdgeInsetsMake(0, 7, 0, -7);
    _navImage.frame = CGRectMake((SCREEN_W-imageW)/2.0, jianGeH+(44-imageH)/2.0, imageW, imageH);
    _navImage.image = [UIImage imageNamed:@"@touxiang"];
    _navView.hidden = YES;
    [self.view bringSubviewToFront:_navView];
}

-(void)loadUI
{
//    _webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    _webView.scrollView.showsVerticalScrollIndicator = NO;
    _webView.scrollView.showsHorizontalScrollIndicator = NO;
    CGFloat buttonW = 55;
    CGFloat buttonH = buttonW;
    _xuanFuButton.frame = CGRectMake(SCREEN_W-buttonW, SCREEN_H-64-buttonH, buttonW, buttonH);
    _xuanFuButton.titleLabel.font = [UIFont systemFontOfSize:12];
    
    CALayer *layer = [CALayer layer];
    layer.frame = _xuanFuButton.frame;
    layer.backgroundColor = [UIColor whiteColor].CGColor;
    layer.shadowOffset = CGSizeMake(0, 0);
    layer.shadowColor =RGBColor(105, 105, 105).CGColor;
    layer.shadowOpacity = 0.9;
    layer.cornerRadius = buttonH/2.0;
    [self.view.layer addSublayer:layer];
    [self.view bringSubviewToFront:_xuanFuButton];
    
    [_xuanFuButton setYuanJiaoForRadius:buttonH/2.0 andMasks:NO];
    _xuanFuButton.backgroundColor = RGBColorAlpha(44, 44, 44, 1);
    [_xuanFuButton setTitle:@"进入\n量体" forState:UIControlStateNormal];
    [_xuanFuButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _headerView.frame =CGRectMake(0, 0, SCREEN_W, 120);
//    _headerView.backgroundColor = [UIColor greenColor];
    CGFloat newsViewH = 80;
    CGFloat jianGeW = 25;
    CGFloat jianGeH = 10;
    _newsView.frame  = CGRectMake(0, AdView_H, SCREEN_W, newsViewH);
    _news_lineView.frame = CGRectMake(jianGeW, 0, SCREEN_W-jianGeW*2, 1);
    _news_lineView.backgroundColor = RGBColorAlpha(151, 151, 151, 0.3);
    _news_nameLabel.text = @"长款男士风衣系列第一款";
    _news_nameLabel.textColor = ShenZi_Color;
    _news_nameLabel.font = Cell_Big_Font;
    [_news_nameLabel sizeToFit];
    _news_nameLabel.frame = CGRectMake(jianGeW, jianGeH, _news_nameLabel.frame.size.width, Nomal_Label_H);
    
    CGFloat imageW = 20;
    CGFloat imageH = 20;
    _news_countLabel.text = @"1111234";
    _news_countLabel.textColor = RGBColorAlpha(237, 67, 24, 1);
    _news_countLabel.font = Cell_Little_Font;
    [_news_countLabel sizeToFit];
    _news_countLabel.frame = CGRectMake(SCREEN_W-_news_countLabel.frame.size.width-jianGeW,jianGeH, _news_countLabel.frame.size.width, Nomal_Label_H);
    _news_countImage.frame = CGRectMake(SCREEN_W-jianGeW-_news_countLabel.frame.size.width-imageW, jianGeH, imageW, imageH);
    _news_countImage.image = [UIImage imageNamed:@"yan"];
    _news_countImage.contentMode = UIViewContentModeScaleAspectFit;
    _news_contentLabel.text = [NSString stringWithFormat:@"生产周期：%@       款式号：%@",@"7个工作日",@"GHKHKJK"];
    _news_contentLabel.textColor = QianZi_color;
    _news_contentLabel.font = Cell_Little_Font;
    [_news_contentLabel sizeToFit];
    _news_contentLabel.frame = CGRectMake(jianGeW, _news_nameLabel.frame.origin.y+_news_contentLabel.frame.size.height+5, _news_contentLabel.frame.size.width, Nomal_Label_H);
    
    CGFloat viewH = 80;
    buttonW = 20;
    CGFloat c_imageW = SCREEN_W-buttonW-jianGeW*2-10;
    CGFloat c_imageH = c_imageW/(1870/32.);
    buttonH = c_imageH;
    _CustomNewsView.frame  =CGRectMake(0, _newsView.frame.origin.y+_newsView.frame.size.height, SCREEN_W, viewH);
    _c_headView.frame = CGRectMake(0, 0, SCREEN_W, 20);
    _c_headImage.frame = CGRectMake(jianGeW, 0, c_imageW , c_imageH);
    _c_headImage.image = [UIImage imageNamed:@"@dingzhi"];
    _c_headButton.frame = CGRectMake(SCREEN_W-jianGeW-buttonW, 0, buttonW, buttonH);
    _c_otherView.frame =CGRectMake(0, _c_headView.frame.origin.y+_c_headView.frame.size.height, SCREEN_W, 60);
    [self loadCustomView:_c_otherView];
    _PersonalCustomView.frame  =CGRectMake(0, _CustomNewsView.frame.origin.y+_CustomNewsView.frame.size.height, SCREEN_W, viewH);
    _p_headView.frame = CGRectMake(0, 0, SCREEN_W, viewH);
    _p_headImage.frame = CGRectMake(jianGeW, 0, c_imageW , c_imageH);
    _p_headImage.image = [UIImage imageNamed:@"@siren"];
    _p_headButton.frame = CGRectMake(SCREEN_W-jianGeW-buttonW, 0, buttonW, buttonH);
    
    _p_otherView.frame =CGRectMake(0, _p_headView.frame.origin.y+_p_headView.frame.size.height, SCREEN_W, 60);
    _webView.frame = CGRectMake(0, 44, SCREEN_W, SCREEN_H-4);
    _adView = [[ADView alloc]init];
    [self.headerView addSubview:_adView];
    [self loadADView];
    [_webView.scrollView addSubview:_headerView];
}
#pragma mark -  竖屏适配
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    CGFloat newsViewH = 80;
//    CGFloat viewH = 80;
    _webView.frame = CGRectMake(0, 44, SCREEN_W, SCREEN_H-4);
    CGFloat buttonW = 55;
    CGFloat buttonH = buttonW;
    _xuanFuButton.frame = CGRectMake(SCREEN_W-buttonW, SCREEN_H-64-buttonH, buttonW, buttonH);
    _headerView.frame =CGRectMake(0, 0, SCREEN_W, 120);
    [self loadADView];
    _newsView.frame  = CGRectMake(0, AdView_H, SCREEN_W, newsViewH);
    _CustomNewsView.frame = CGRectMake(0, _newsView.frame.origin.y+_newsView.frame.size.height, SCREEN_W, newsViewH);
    
    
}
-(void)loadCustomView:(UIView*)view
{
    
}

-(void)loadADView//设置广告位
{
    CGFloat adView_H = AdView_H;
    NSMutableArray * arr = [[NSMutableArray alloc]init];
    NSArray * imageArr = [NSArray arrayWithObjects:@"nv_c1",@"nv_c2",@"nv_c3", nil];
    for (int i=0; i<imageArr.count; i++) {
        ADModel* model = [[ADModel alloc]init];
        model.cover = [imageArr objectAtIndex:i];
        //adView内部的默认图片改了，记得改回来。
        [arr addObject:model];
    }
    self.AdArray = arr;
    _adView.frame = CGRectMake(0, 0,SCREEN_W ,adView_H);
    [_adView loadADViewWithNumber:[self.AdArray count] WithImageFrame:CGSizeMake(SCREEN_W, adView_H) WithHorizontal:YES WithAnimationTime:2.0];
    _adView.backgroundColor=[UIColor whiteColor];
    _adView.tag = ADVIEW_TAG;
    NSMutableArray *tmpImageArray = [[NSMutableArray alloc] init];
    for (int i=0; i<[self.AdArray count]; i++) {
        ADModel* model = [self.AdArray objectAtIndex:i];
        NSString *productADString  = model.cover;
        [tmpImageArray addObject:productADString];
    }
//    _adView.imageColor = [UIColor whiteColor];
    if ([self.AdArray count]>0) {
        [_adView initNOFitImageViewWithImagesURL:tmpImageArray andPlaceholderImageName:DEFAULT_HOMEAD_PHOTO_RECTANGLE];
    }
    [_adView clickOneImage:^(NSInteger index)
     {
         //点击图片，然后
         if (index < [self.AdArray count])
         {
             
             ADModel* model = [self.AdArray objectAtIndex:index];
             if (GlobalHandleValue(model.jump) == nil || [GlobalHandleValue(model.jump) isEqualToString:@""]) {
                 return ;
             }
             
         }
         
         
     }];
    
    [_adView addPageControl];
    [_adView startAnimation:YES];
    
}

#pragma mark -  scrollDelegate
// 完成拖拽(滚动停止时调用此方法，手指离开屏幕前)
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    CGFloat ViewH = 64;
    CGFloat View_H = 0;
    CGFloat velocity = scrollView.contentOffset.y;
    if (velocity >0) {
        _navView.hidden = NO;
        //向下拖动，显示导航栏
        [UIView animateWithDuration:0.37 animations:^{
            
            _navView.frame = CGRectMake(0, -View_H, SCREEN_W, ViewH);
        }];
        
    }else if (velocity <=0) {
        //向上拖动，隐藏导航栏
        [UIView animateWithDuration:0.37 animations:^{
            _navView.frame = CGRectMake(0, -View_H, SCREEN_W, 0);
        } completion:^(BOOL finished) {
            _navView.hidden = YES;
        }];
        
        
    }
    
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat ViewH = 64;
    CGFloat View_H = 0;
    CGFloat velocity = scrollView.contentOffset.y;
    if (velocity >0) {
        _navView.hidden = NO;
        //向下拖动，显示导航栏
        [UIView animateWithDuration:0.37 animations:^{
            
            _navView.frame = CGRectMake(0, -View_H, SCREEN_W, ViewH);
        }];
        
    }else if (velocity <=0) {
        //向上拖动，隐藏导航栏
        [UIView animateWithDuration:0.37 animations:^{
            _navView.frame = CGRectMake(0, -View_H, SCREEN_W, 0);
        } completion:^(BOOL finished) {
            _navView.hidden = YES;
        }];
        
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -  手势方法按钮
- (IBAction)c_headButton:(UIButton *)sender {
}
- (IBAction)p_headButton:(UIButton *)sender
{
    
}
- (IBAction)backbutton:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)xuanFuButton:(UIButton *)sender {
    HomeEssentialViewController * tmpVC = [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:@"HomeEssentialViewController"];
    tmpVC.title = @"基本信息";
    tmpVC.hidesBottomBarWhenPushed  = NO;
    [self.navigationController pushViewController:tmpVC animated:YES];
}

@end
